package plugin.bundle.impl;

import org.apache.commons.collections.CollectionUtils;
import plugin.bundle.IEventBundle;
import plugin.event.IBaseEvent;
import plugin.event.buss.IRefundEvent;
import plugin.event.buss.ZhiFuEvent;
import plugin.event.buss.impl.IRefundEventImpl;
import plugin.event.buss.impl.ZhiFuEventImpl;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 峥桂 on 2017/4/26.
 */
public class EventBundle implements IEventBundle{

    List<IBaseEvent> processor;

    public EventBundle(){
        if(CollectionUtils.isEmpty(processor)){
            processor=new ArrayList<>();
            processor.add(new ZhiFuEventImpl());
            processor.add(new IRefundEventImpl());
        }
    }

    private <T> T getMyEvent(Class<T> tClass){
        T t=null;
        for(IBaseEvent iBaseEvent:processor)
            if (tClass.isInstance(iBaseEvent)) {
                t = (T)iBaseEvent;
                break;
            }
        return t;
    }



    @Override
    public boolean pay() {
        getMyEvent(ZhiFuEvent.class).pay();
        return false;
    }

    @Override
    public boolean refund() {
        getMyEvent(IRefundEvent.class).refund();
        return false;
    }
}
